home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4568 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.6 KB

  1. Path: interramp.com!usenet
  2. From: Brian Strelioff <pp001729@interramp.com>
  3. Newsgroups: comp.lang.java,comp.lang.eiffel,comp.lang.misc,comp.lang.c++
  4. Subject: Re: Constness in languages
  5. Date: Tue, 30 Jan 1996 16:50:38 -0800
  6. Organization: BKS Systems, Inc.
  7. Message-ID: <310EBCDE.32DE@interramp.com>
  8. References: <30FBED5F.28B8@achilles.net> <4dj4t4$a4@kai.com> <4e6dtm$si5@gaia.ns.utk.edu> <9602901.11916@mulga.cs.mu.OZ.AU> <4ej1eo$s7@stc06.ctd.ornl.gov>
  9. NNTP-Posting-Host: ip137.los-angeles6.ca.interramp.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b6a (X11; I; SunOS 5.5 sun4m)
  14.  
  15. Another way to view "const" is as a language rule. For example,
  16. "2 := 3" is usually not allowed, and not many folks argue
  17. that it should be allowed: clearly there is a well understood
  18. "const" quality to literals. 
  19.  
  20. In C/C++, const value arguments are useful as a safety check,
  21. particularly given the increased use of macros which may hide
  22. attempts to change the value of an argument. const references
  23. & pointers (actually pointers to const) are also extremely
  24. useful from an API specification standpoint as they clearly
  25. point out (and enforce assuming no casting is used) which
  26. arguments can/will be changed by a routine.
  27.  
  28. In Eiffel, values of arguments are already "const", but there is still
  29. the question of how to make references "const". Pre/Post-conditions
  30. help to some degree, but not only does this defer detection until
  31. run-time it also makes enforcement "optional" since checking
  32. of pre/post conditions is itself optional. Also, lets look at
  33. a relatively simple example of ensuring that a STRING is not
  34. modified:
  35.  class xxx
  36.  ...
  37.     no_touch(s: STRING) is
  38.       require
  39.         s_exists: s /= Void
  40.       do
  41.         ...
  42.       ensure
  43.         ???
  44.       end
  45.  end
  46.  
  47. How do we encode the (dynamically checked) postcondition?
  48.  
  49. 1) s = old s -- won't work since this just checks that the object
  50. referenced by s is the same object, not that the object didn't change
  51. (Since the value of s cannot change, this check in fact is worthless)
  52.  
  53. 2) s.is_equal(old s) -- won't work either (since s = old s, the
  54. field-by-field comparison of s and itself is essentially useless)
  55.  
  56. 3) declare a local string, copy s into at, and check at the end
  57. of the routine? Can't do it since Eiffel (the language) doesn't
  58. allow use of locals in postconditions (NOTE: some vendors do allow
  59. this as an vendor dependant extension)
  60.  
  61. 4) add a field to xxx where s is copied into and check at the end
  62. of no_touch? Doable, but ugly and slow.
  63.  
  64. OTOH, in C/C++ this specification is easy and statically checkable:
  65.  no_touch(String const * const s) {
  66.    ...
  67.  }
  68.  
  69. [Note const routines (i.e. routines that do not change the object
  70. used to invoke them) are an automatic necessity once const arguments
  71. enter the language]
  72.  
  73. Granted the initial introduction of "const" into an existing class
  74. library is painful, but I think definitely worth it. For classes
  75. being written from scratch I find there to be no incremental cost
  76. in appropriate use of "const" arguments.
  77.  
  78. As to const function results, I think it necessary to allow for them.
  79. For example, given CONTAINER[Const STRING], any function returning
  80. an item from that container should return it as a "Const STRING".
  81.  
  82. Yes it may be possible to emulate const in Eiffel via duplicate
  83. class definitions (i.e. writable, read-only), but not completely
  84. and not efficiently (both in development and execution).
  85.  
  86. What I would like to see in both Eiffel & Java is "const constness"
  87. in which the semantics of C/C++ const are available without the
  88. ability to override (via casting in C/C++, inheritance in Eiffel).
  89.